<template>
  <el-dialog
    class="subject-status-add-record"
    :title="dialogTitle"
    width="600px"
    :show-close="true"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <div>
      <el-form :rules="rules" ref="formRef" size="mini" :model="formData" label-width="140px">
        <el-form-item label="来源：">
          <el-radio-group v-model="formData.source">
            <el-radio label="1">S</el-radio>
            <el-radio label="2">P</el-radio>
            <el-radio label="3">M</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="编码组：" prop="codeGroup">
          <el-input style="width: 80%" clearable placeholder="请输入" v-model.trim="formData.codeGroup"> </el-input>
        </el-form-item>
        <el-form-item label="编码组中文：" prop="codeGroupCn">
          <el-input style="width: 80%" clearable placeholder="请输入" v-model="formData.codeGroupCn"> </el-input>
        </el-form-item>
        <el-form-item label="编码名：" prop="codeName">
          <el-input
            :disabled="dialogTitle === '编辑'"
            style="width: 80%"
            clearable
            placeholder="请输入"
            v-model="formData.codeName"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="编码名中文：" prop="codeNameCn">
          <el-input style="width: 80%" clearable placeholder="请输入" v-model="formData.codeNameCn"> </el-input>
        </el-form-item>
        <el-form-item label="编码值：" prop="codeValue">
          <el-input
            :disabled="dialogTitle === '编辑'"
            style="width: 80%"
            clearable
            placeholder="请输入"
            v-model="formData.codeValue"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="编码描述：" prop="codeDescribe">
          <el-input style="width: 80%" clearable placeholder="请输入" v-model="formData.codeDescribe"> </el-input>
        </el-form-item>
        <el-form-item label="生效状态：" prop="effectuate">
          <el-radio-group v-model="formData.effectuate">
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="父级编码名称：">
          <el-input style="width: 80%" clearable placeholder="请输入" v-model="formData.fatherCodeName"> </el-input>
        </el-form-item>
        <el-form-item label="父级编码值：">
          <el-input style="width: 80%" clearable placeholder="请输入" v-model="formData.fatherCodeValue"> </el-input>
        </el-form-item>
        <el-form-item label="排序：">
          <!-- <el-input style="width: 80%" clearable placeholder="请输入" v-model="formData.sort">
          </el-input> -->
          <el-input-number
            style="width: 80%"
            clearable
            :step="1"
            step-strictly
            v-model="formData.sort"
          ></el-input-number>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button :loading="loading" size="small" type="primary" @click="handleSave">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapActions } from 'vuex'

export default {
  props: {
    editData: Object,
    dialogVisible: Boolean,
    dialogTitle: String,
    searchParams: Object,
  },
  data() {
    return {
      rules: {
        source: [{ required: true, message: '请输入', trigger: 'blur' }],
        codeGroup: [{ required: true, message: '请输入', trigger: 'blur' }],
        codeGroupCn: [{ required: true, message: '请输入', trigger: 'blur' }],
        codeName: [{ required: true, message: '请输入', trigger: 'blur' }],
        codeNameCn: [{ required: true, message: '请输入', trigger: 'blur' }],
        codeValue: [{ required: true, message: '请输入', trigger: 'blur' }],
        codeDescribe: [{ required: true, message: '请输入', trigger: 'blur' }],
        effectuate: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
      loading: false,
      defaultFormData: {
        effectuate: '0',
      },
      formData: {},
    }
  },

  watch: {
    dialogVisible(val) {
      if (val) {
        this.formData = this.editData.id ? { ...this.editData } : { ...this.defaultFormData }
      } else {
        this.formData = { ...this.defaultFormData }
      }
    },
  },

  methods: {
    ...mapActions('eCRFDesign', ['addGeneralCoding', 'editGeneralCoding']),

    // 关闭取消
    handleClose() {
      this.formData = { ...this.defaultFormData }
      this.$emit('handleCancel')
    },

    // 保存提交
    handleSave() {
      this.loading = true
      this.$refs.formRef.validate(valid => {
        if (valid) {
          const data = {
            ...this.formData,
            ...this.searchParams,
            environment: 0
          }
          if (data.id && this.dialogTitle !== '复制添加') {
            this.editGeneralCoding({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message({
                  message: '保存成功',
                  type: 'success',
                })
                this.$refs.formRef.resetFields()
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          } else {
            this.addGeneralCoding({ data }).then(res => {
              const { code } = res
              if (code === 200) {
                this.$message({
                  message: '保存成功',
                  type: 'success',
                })
                this.$refs.formRef.resetFields()
                this.$emit('handleCancel', 'refresh')
              }
              this.loading = false
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
@import './add-record.scss';
</style>
